package com.attendance.www.hr.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.attendance.www.hr.beans.ModifyLogsBean;
import com.attendance.www.hr.beans.ModifyLogsPageBean;
import com.attendance.www.hr.dao.mapper.EmployeeMapper;
import com.attendance.www.hr.dao.mapper.FurloughBalanceMapper;
import com.attendance.www.hr.dao.mapper.ModifyLogsExtMapper;
import com.attendance.www.hr.dao.mapper.ModifyLogsMapper;
import com.attendance.www.hr.entitys.Employee;
import com.attendance.www.hr.entitys.FurloughBalance;
import com.attendance.www.hr.entitys.FurloughBalanceExample;
import com.attendance.www.hr.entitys.ModifyLogs;
import com.attendance.www.hr.entitys.ModifyLogsExample;
import com.attendance.www.hr.service.IModifyService;

@Service
public class ModifyServiceImpl implements IModifyService {
	
	@Resource
	private ModifyLogsMapper modifyLogsDao;
	@Resource
	private EmployeeMapper employeeDao;
	@Resource
	private FurloughBalanceMapper furloughBalanceDao;
	@Resource
	private ModifyLogsExtMapper modifyLogsExtDao;

	/**
	 * 查询员工调休剩余额度
	 */
	@Override
	public ModifyLogsPageBean getRestBalanceList(ModifyLogsBean modifyLogsBean) {
		ModifyLogsPageBean mlpb = new ModifyLogsPageBean();
		mlpb.setModifyLogsBeanList(modifyLogsExtDao.getRestBalanceList(new Date(), modifyLogsBean));
		mlpb.setPageTotal(modifyLogsExtDao.count(modifyLogsBean));
		return mlpb;
	}
	
	/**
	 * 查询调整调休额度logs
	 */
	@Override
	public List<ModifyLogsBean> getModifyLogsList(String employeeNo) {
		// TODO Auto-generated method stub
		ModifyLogsExample mExample=new ModifyLogsExample();
		mExample.createCriteria().andEmployeeNoEqualTo(employeeNo);
		List<ModifyLogs> modifyLogsList=modifyLogsDao.selectByExample(mExample);
		List<ModifyLogsBean> mlbList=new ArrayList<>();
		for (ModifyLogs modifyLogs : modifyLogsList) {
			ModifyLogsBean modifyLogsBean=new ModifyLogsBean();
			modifyLogsBean.setChineseName(modifyLogs.getChineseName());
			modifyLogsBean.setCreateTime(modifyLogs.getCreateTime());
			modifyLogsBean.setEmployeeNo(modifyLogs.getEmployeeNo());
			modifyLogsBean.setMinutes(modifyLogs.getMinutes());
			if (modifyLogs.getMinutes().compareTo(new BigDecimal(0))>=0) {
				modifyLogsBean.setMinutesStr("+"+modifyLogs.getMinutes());
			} else {
				modifyLogsBean.setMinutesStr(modifyLogs.getMinutes().toString());
			}
			modifyLogsBean.setModifyChineseName(modifyLogs.getModifyChineseName());
			modifyLogsBean.setModifyEmployeeNo(modifyLogs.getModifyEmployeeNo());
			modifyLogsBean.setDescription(modifyLogs.getDescription());
			mlbList.add(modifyLogsBean);
		}
		return mlbList;
	}

	/**
	 * 调整调休余额；并新增调整调休（加班）额度记录
	 */
	@Override
	public boolean saveModifyLogs(ModifyLogsBean modifyLogsBean) {
		// TODO Auto-generated method stub
		//被调整人的ID
		Integer employeeId=modifyLogsBean.getEmployeeId();
		//调整调休余额
		FurloughBalanceExample example=new FurloughBalanceExample();
		example.createCriteria().andEmployeeIdEqualTo(employeeId)
		.andFurloughBalanceTypeEqualTo(0);
		FurloughBalance furloughBalance=new FurloughBalance();
		furloughBalance.setBalanceHoursDay(modifyLogsBean.getAfterBalanceHoursDay());
		int record=furloughBalanceDao.updateByExampleSelective(furloughBalance, example);
		if (record<=0) {
			return false;
		}
		
		//做调整操作人的ID
		Integer modifyEmployeeId=modifyLogsBean.getModifyEmployeeId();
		//新增调整调休（加班）额度记录
		ModifyLogs modifyLogs=new ModifyLogs();
		modifyLogs.setChineseName(modifyLogsBean.getChineseName());
		modifyLogs.setEmployeeNo(modifyLogsBean.getEmployeeNo());
		modifyLogs.setMinutes(modifyLogsBean.getMinutes());
		modifyLogs.setDescription(modifyLogsBean.getDescription());
		if (modifyEmployeeId==null) {//管理员
			modifyLogs.setModifyChineseName("admin");
		} else {
			Employee employee=employeeDao.selectByPrimaryKey(modifyEmployeeId);
			modifyLogs.setModifyChineseName(employee.getChineseName());
			modifyLogs.setModifyEmployeeNo(employee.getEmployeeNo());
		}
		modifyLogs.setModifyEmployeeNo(modifyLogsBean.getModifyEmployeeNo());
		record=modifyLogsDao.insertSelective(modifyLogs);
		if (record<=0) {
			return false;
		}
		return true;
	}
}
